#ifndef OSG_TEMPLATE_TEMPLATE_UTIL_H
#define OSG_TEMPLATE_TEMPLATE_UTIL_H
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> //事件监听
#include <osgGA/StateSetManipulator> //事件响应类，对渲染状态进行控制

int previewImage(osg::ref_ptr<osg::Image> image)
{
    osg::ref_ptr<osg::Geometry> quad = osg::createTexturedQuadGeometry(
        osg::Vec3(-1.0f, 0.0f, -1.0f), // 左下角顶点
        osg::Vec3(2.0f, 0.0f, 0.0f),   // 宽度方向向量
        osg::Vec3(0.0f, 0.0f, 2.0f),   // 高度方向向量
        0.0f, 0.0f, 1.0f, 1.0f         // 纹理坐标范围 (smin, tmin, smax, tmax)
    );

    // 创建一个纹理对象
    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    texture->setImage(image); // 加载纹理图片

    // 创建一个状态集并设置纹理
    osg::ref_ptr<osg::StateSet> stateset = quad->getOrCreateStateSet();
    stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

    // 创建一个Geode节点并将几何体添加到其中
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(quad);

    // 创建Viewer并设置场景数据
    osgViewer::Viewer viewer;
    viewer.setSceneData(geode);
    //切换网格模式，方便比较
    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
    viewer.addEventHandler(new osgViewer::StatsHandler());//实现状态信息统计
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());

    viewer.setUpViewInWindow(400, 400, 1000, 800);
    // 运行Viewer
    return viewer.run();
}
#endif // !OSG_TEMPLATE_TEMPLATE_UTIL_H
